//
//  MZExtensionDate.swift
//  MZTool
//
//  Created by gby on 2022/1/25.
//

import Foundation


extension MZKit where Base == Date {
    public var day: Int {
        get{
            return Calendar.current.component(.day, from: base)
        }
    }
    public var month: Int {
        get{
            return Calendar.current.component(.month, from: base)
        }
    }
    
    public var year: Int {
        get{
            return Calendar.current.component(.year, from: base)
        }
    }
    
    public var hour: Int {
        get{
            return Calendar.current.component(.hour, from: base)
        }
    }
    
    public var minute: Int {
        get{
            return Calendar.current.component(.minute, from: base)
        }
    }
    
    public var second: Int {
        get{
            return Calendar.current.component(.second, from: base)
        }
    }
    
    public var nanosecond: Int {
        get{
            return Calendar.current.component(.nanosecond, from: base)
        }
    }
    
    public var weakday: Int {
        get{
            return Calendar.current.component(.weekday, from: base)
        }
    }
    
    public var weakdayOrdinal: Int {
        get{
            return Calendar.current.component(.weekdayOrdinal, from: base)
        }
    }
    
    public var weakOfMonth: Int {
        get{
            return Calendar.current.component(.weekOfMonth, from: base)
        }
    }
    
    public var weekOfYear: Int {
        get{
            return Calendar.current.component(.weekOfYear, from: base)
        }
    }
    
    public var yearForWeekOfYear: Int {
        get{
            return Calendar.current.component(.yearForWeekOfYear, from: base)
        }
    }
    
    public var quarter: Int {
        get{
            return Calendar.current.component(.quarter, from: base)
        }
    }
    
    public func isToday() -> Bool {
        if fabs(base.timeIntervalSinceNow) >= 60*60*24 {
            return false
        }
        return Date().mz.day == self.day
    }
    
    public func isYesterDay() -> Bool {
        return self.dateByAddingDays(1)?.mz.isToday() ?? false
    }
    
    public func dateByAddingDays(_ days: Int) -> Date? {
        var components = DateComponents.init()
        components.day = days
        return Calendar.current.date(byAdding: components, to: base)
    }
    
    public func dateStr(format: String) -> String? {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        return formatter.string(from: base)
    }
    
    public func dateStr_Beijing(format: String) -> String? {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        formatter.timeZone = TimeZone.init(secondsFromGMT: 8*3600);
        return formatter.string(from: base)
    }
    
    public func monthLastDay(month: Int) -> Date?{
        var components = Calendar.current.dateComponents([.day,.month,.year,.hour,.minute,.second], from: base)
        components.day = 1
        components.month = (components.month ?? 0) + month
        components.hour = 23
        components.minute = 59
        components.second = 59
        guard let d = Calendar.current.date(from: components) else {
            return nil
        }
        components.day = Calendar.current.range(of: .day, in: .month, for: d)?.count
        return Calendar.current.date(from: components)
    }
    
    public func monthFirstDay(month: Int) -> Date?{
        var components = Calendar.current.dateComponents([.day,.month,.year,.hour,.minute,.second], from: base)
        components.day = 1
        components.month = (components.month ?? 0) + month
        components.hour = 0
        components.minute = 0
        components.second = 0
        return Calendar.current.date(from: components)
    }
    
    
    public func compareCurretnTime() -> String{
        var timeTips = "刚刚"
        let currentDate = Date.init()
        let seconds: TimeInterval = currentDate.timeIntervalSince(base)
        let perMinSec: Double = 60
        let perHourSec: Double = 60*60
        let perDaySec: Double = 60*60*24
        if seconds < perHourSec {
            let mins = Int(seconds/perMinSec)
            if mins > 0{
                timeTips = "\(mins)" + "分钟前"
            }else{
                timeTips = "刚刚"
            }
        }else if seconds >= perHourSec, seconds <  perDaySec {
            let hours = Int(seconds/perHourSec)
            if hours <= 1 {
                timeTips = "1小时前"
            }else{
                timeTips = "\(hours)" + "小时前"
            }
        }else if seconds > perDaySec , seconds <= 3 * perDaySec{
            let days = Int(seconds/perDaySec)
            if days <= 1 {
                timeTips = "1天前"
            }else{
                timeTips = "\(days)" + "天前"
            }
        }else {
            let baseyear: Int = base.mz.year
            let current_year: Int = currentDate.mz.year
            if baseyear == current_year {
                timeTips = base.mz.dateStr(format: "MM/dd") ?? ""
            }else{
                timeTips = base.mz.dateStr(format: "yyyy/MM/dd") ?? ""
            }
        }
        return timeTips
    }
    
    
    
    
    public static func dateForm(str: String, format: String? = "yyyy-MM-dd HH:mm:ss") -> Date? {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        return formatter.date(from: str)
    }
    
    public static func timeStrFrom(seconds: Int64, format: String? = "yyyy-MM-dd HH:mm:ss") -> String? {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        let date = Date.init(timeIntervalSince1970: TimeInterval(seconds))
        return formatter.string(from: date)
    }
}
